﻿using System.Collections;

namespace Chap11_jihe
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 浅度复制
            Cloner mySource = new Cloner(5);
            Cloner myTarget = (Cloner)mySource.GetCopy();
            Console.WriteLine($"myTarget.MyContent.Val={myTarget.myContent.Val}");
            mySource.myContent.Val = 2;
            Console.WriteLine($"myTarget.MyContent.Val={myTarget.myContent.Val}"); 
            #endregion
            Console.ReadKey();


            Console.WriteLine("Hello, World!");
            List<Animal> animalList = new List<Animal>();
            Animals animals = new Animals();
            animals.Add(new Cow("MooMoo"));
            animals.Add(new Chicken("Chicken"));
            foreach (Animal animal in animals)
            {
                Console.WriteLine($"New {animal.ToString()} object add to custom collection,Name = {animal.Name}");
            }
            Console.ReadKey();
        }
    }

    //public class Animals : IEnumerable<Animal>
    //{
    //    private readonly List<Animal> _animals = new List<Animal>();

    //    public void Add(Animal animal) => _animals.Add(animal);
    //    public void Remove(Animal animal) => _animals.Remove(animal);

    //    public IEnumerator<Animal> GetEnumerator()
    //    {
    //        return _animals.GetEnumerator();
    //    }

    //    IEnumerator IEnumerable.GetEnumerator()
    //    {
    //        return GetEnumerator();
    //    }

    //    //public IEnumerator<Animal> GetEnumerator() => _animals.GetEnumerator();
    //    //IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

    //    public Animals()
    //    {
    //        // 初始化逻辑
    //    }
    //}

    //public class Animals : List<Animal>
    //{      

    //    public Animals()
    //    {

    //    }
    //}
    public class Animals : CollectionBase
    {
        public void Add(Animal animal) => List.Add(animal);

        public void Remove(Animal animal) => List.Remove(animal);

        #region 这段代码的作用可以让我们通过索引访问集合中的元素
        public Animal this[int index]
        {
            get { return (Animal)List[index]; }
            set { List[index] = value; }
        } 
        #endregion

        public Animals()
        {

        }
    }

    public abstract class  Animal
    {
        protected string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Animal() => name = "The animal with no name";
        
        public Animal(string name) => this.name = name;

        public void Feed() => Console.WriteLine($"{name} has been fed.");
    } 

    public class Cow : Animal
    {
        public void Milk() => Console.WriteLine($"{name} has been milked.");

        public Cow(string newName) : base(newName) { }
    }

    public class Chicken : Animal
    {
        public void LayEgg() => Console.WriteLine($"{name} has laid an egg.");

        public Chicken(string newName) : base(newName) { }
    }






    public class Vector
    {
        public double? R = null;
        public double? Theta = null;

        public double? ThetaRadians
        {
            get
            {
                return (Theta * double.Pi / 180.0);
            }
        }

        public Vector(double? r, double? theta)
        {
            if (r < 0)
            {
                r = -r;
                theta = theta + 180;
            }
            theta = theta % 360;
            R = r;
            Theta = theta;
        }
    }
}
